package com.ruoyi.sl.service.impl;

import java.util.List;

import cn.hutool.core.date.DateTime;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlOfcLeave;
import com.ruoyi.sl.mapper.SlLevelMapper;
import com.ruoyi.sl.mapper.SlOfcLeaveMapper;
import com.ruoyi.sl.service.ISlMonthOfcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sl.mapper.SlLeaveRecordMapper;
import com.ruoyi.sl.domain.SlLeaveRecord;
import com.ruoyi.sl.service.ISlLeaveRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 请假记录Service业务层处理
 * 
 * @author admin
 * @date 2024-06-14
 */
@Service
public class SlLeaveRecordServiceImpl implements ISlLeaveRecordService 
{
    @Autowired
    private SlLeaveRecordMapper slLeaveRecordMapper;

    @Autowired
    private ISlMonthOfcService slMonthOfcService;

    /**
     * 查询请假记录
     * 
     * @param leaveRecordId 请假记录主键
     * @return 请假记录
     */
    @Override
    public SlLeaveRecord selectSlLeaveRecordByLeaveRecordId(Long leaveRecordId)
    {
        return slLeaveRecordMapper.selectSlLeaveRecordByLeaveRecordId(leaveRecordId);
    }
    /**
     * 查询请假记录
     *
     * @param ofcmpayId 工资主键
     * @return 请假记录集合
     */
    @Override
    public List<SlLeaveRecord> selectSlLeaveRecordByOfcmpayId(Long ofcmpayId){
        return slLeaveRecordMapper.selectSlLeaveRecordByOfcmpayId(ofcmpayId);
    }
    /**
     * 查询请假记录列表
     * 
     * @param slLeaveRecord 请假记录
     * @return 请假记录
     */
    @Override
    public List<SlLeaveRecord> selectSlLeaveRecordList(SlLeaveRecord slLeaveRecord)
    {
        return slLeaveRecordMapper.selectSlLeaveRecordList(slLeaveRecord);
    }

    /**
     * 新增请假记录
     * 
     * @param slLeaveRecord 请假记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSlLeaveRecord(SlLeaveRecord slLeaveRecord)
    {
        slLeaveRecord.setCreateTime(DateUtils.getNowDate());
        int rows = slLeaveRecordMapper.insertSlLeaveRecord(slLeaveRecord);

        slMonthOfcService.updateOfcMonthLeavePay(slLeaveRecord.getOfcmpayId());

        return rows;
    }

    /**
     * 修改请假记录
     * 
     * @param slLeaveRecord 请假记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlLeaveRecord(SlLeaveRecord slLeaveRecord)
    {
        slLeaveRecord.setUpdateTime(DateUtils.getNowDate());
        int rows = slLeaveRecordMapper.updateSlLeaveRecord(slLeaveRecord);
        slMonthOfcService.updateOfcMonthLeavePay(slLeaveRecord.getOfcmpayId());
        return rows;
    }

    /**
     * 批量删除请假记录
     * 
     * @param leaveRecordIds 需要删除的请假记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlLeaveRecordByLeaveRecordIds(Long[] leaveRecordIds)
    {
        //获取删除的对应的工资ID
        SlLeaveRecord slLeaveRecord = slLeaveRecordMapper.selectSlLeaveRecordByLeaveRecordId(leaveRecordIds[0]);
        Long ofcmpayId = slLeaveRecord.getOfcmpayId();

        int rows = slLeaveRecordMapper.deleteSlLeaveRecordByLeaveRecordIds(leaveRecordIds);

        slMonthOfcService.updateOfcMonthLeavePay(ofcmpayId);

        return rows;
    }

    /**
     * 删除请假记录信息
     * 
     * @param leaveRecordId 请假记录主键
     * @return 结果
     */
    @Override
    public int deleteSlLeaveRecordByLeaveRecordId(Long leaveRecordId)
    {
        return slLeaveRecordMapper.deleteSlLeaveRecordByLeaveRecordId(leaveRecordId);
    }
}
